/*
 * @FileName: [ComboCardTradeServiceImp.java] 
 * @Package com.bcinfo.iccard.service.impl 
 * 
 * 
 * Copyright (c) 2011-2015 BCINFO Technology Limited Com.
 * All rights reserved.
 * 
 * This software is the confidential and proprietary 
 * information of BCINFO Technology Limited Company
 * ("Confidential Information"). You shall not disclose 
 * such Confidential Information and shall use it only
 * in accordance with the terms of the contract agreement 
 * you entered into with RKY.
 * 
 * $Rev: 568 $
 * $LastChangedDate: 2013-12-18 15:09:34 +0800 (Wed, 18 Dec 2013) $
 * $LastChangedBy: wanggc $
 * 
 * @category ICCARD
 * @version 1.1
 * @author $Author: wanggc $
 *
 * Change History:[Formatter: author date description] <br/>
 * 1
 * 2
 * 3
*/


package com.bcinfo.iccard.service.impl;

import java.text.MessageFormat;
import java.util.Date;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.bcinfo.iccard.dao.ComboCardExpensiveRecordMapper;
import com.bcinfo.iccard.dao.ComboCardExpensiveRecordTempMapper;
import com.bcinfo.iccard.dao.ComboCardSaleDetailMapper;
import com.bcinfo.iccard.exception.IccardDatabaseException;
import com.bcinfo.iccard.model.ComboCardExpensiveRecord;
import com.bcinfo.iccard.model.ComboCardExpensiveRecordTemp;
import com.bcinfo.iccard.model.ComboCardSaleDetail;
import com.bcinfo.iccard.model.Pos;
import com.bcinfo.iccard.service.ComboCardTradeService;

/**
 *套餐卡消费服务实现 <br/>
 * <p>Description:<br/>
 * <PRE>
 * </PRE>
 * <p>
 */
@Service
public class ComboCardTradeServiceImpl implements ComboCardTradeService
{
    private static Logger _logger=Logger.getLogger(ComboCardTradeServiceImpl.class);
    @Autowired
    private ComboCardExpensiveRecordMapper comboCardExpensiveRecordMapper;
    @Autowired
    private ComboCardExpensiveRecordTempMapper comboCardExpensiveRecordTempMapper;
    @Autowired
    private ComboCardSaleDetailMapper comboCardSaleDetailMapper;
    /* (non-Javadoc)
     * Description: <br/> 
     *
     * @param merchantCode
     * @param qrCode
     * @param serialNum
     * @return
     * @throws IccardDatabaseException 
     * @see com.bcinfo.iccard.service.ComboCardTradeService#feedBackFailedTradeByMerQrCode(java.lang.String, java.lang.String, java.lang.String)
     */
    @Override
    public void feedBackFailedTradeByMer(String merchantCode,String posCode,
            String serialNum) throws IccardDatabaseException
    {
        if(_logger.isDebugEnabled()){
            _logger.debug(MessageFormat.format("feedBackFailedTradeByMer:merchantCode={0},serialNum={1},posCode={2}",merchantCode,serialNum,posCode));
        }
        try
        {
            ComboCardExpensiveRecordTemp temp=comboCardExpensiveRecordTempMapper.loadBySerialNum(serialNum);
            if(temp!=null){
                if(temp.getExpensiveStatus()==1){
                    //已正式消费
                    //套餐卡拨正只需要将大于最后一笔正常交易的所有预消费改为异常，不涉及金额变动
                    //comboCardExpensiveRecordTempMapper.feedBackStatus(merchantCode, posCode, temp.getCombocardExpensiveTempId());
                    //将未消费成功的记录改为异常状态
                    comboCardExpensiveRecordTempMapper.setExceptionStatus(merchantCode, posCode, temp.getCombocardExpensiveTempId());
                }else if(temp.getExpensiveStatus()==0){
                    //预消费
                    temp.setExpensiveStatus(1);
                    temp.setUpdateTime(new Date());
                    //更新成已消费
                    comboCardExpensiveRecordTempMapper.updateByPrimaryKeySelective(temp);
                    //将该预消费插入到已消费表
                    ComboCardExpensiveRecord record=new ComboCardExpensiveRecord();
                    record.setExpensiveDate(new Date());
                    record.setOpUserId(1l);
                    record.setCombocardExpensiveTempId(temp.getCombocardExpensiveTempId());
                    comboCardExpensiveRecordMapper.insertSelective(record);
                }
            }
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new IccardDatabaseException();
        }
    }

    /* (non-Javadoc)
     * Description: <br/> 
     *
     * @param temp
     * @return
     * @throws IccardDatabaseException 
     * @see com.bcinfo.iccard.service.ComboCardTradeService#addTempExpensive(com.bcinfo.iccard.model.CombocardExpensiveRecordTemp)
     */
    @Override
    public ComboCardExpensiveRecordTemp addTempExpensive(ComboCardExpensiveRecordTemp temp)
            throws IccardDatabaseException
    {
        if(_logger.isDebugEnabled()){
            _logger.debug("addTempExpensive:temp="+temp.toString());
        }
        try
        {
            comboCardExpensiveRecordTempMapper.insertSelective(temp);
            return temp;
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new IccardDatabaseException();
        }
    }

    /* (non-Javadoc)
     * Description: <br/> 
     *
     * @param temp
     * @return
     * @throws IccardDatabaseException 
     * @see com.bcinfo.iccard.service.ComboCardTradeService#update(com.bcinfo.iccard.model.CombocardExpensiveRecordTemp)
     */
    @Override
    public int update(ComboCardExpensiveRecordTemp temp) throws IccardDatabaseException
    {
        // TODO Auto-generated method stub
        if(_logger.isDebugEnabled()){
            _logger.debug("update:temp="+temp.toString());
        }
        try
        {
            int rows=comboCardExpensiveRecordTempMapper.updateByPrimaryKey(temp);
            return rows;
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new IccardDatabaseException();
        }
    }

    /* (non-Javadoc)
     * Description: <br/> 
     *
     * @param qrCode
     * @return
     * @throws IccardDatabaseException 
     * @see com.bcinfo.iccard.service.ComboCardTradeService#loadByQrCode(java.lang.String)
     */
    @Override
    public ComboCardSaleDetail loadByQrCode(String qrCode) throws IccardDatabaseException
    {
        if(_logger.isDebugEnabled()){
            _logger.debug(MessageFormat.format("loadByQrCode:qrCode={0}", qrCode));
        }
        try
        {
            ComboCardSaleDetail ccsd=comboCardSaleDetailMapper.loadByQrCode(qrCode);
            return ccsd;
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new IccardDatabaseException();
        }
    }

    /* (non-Javadoc)
     * Description: <br/> 
     *
     * @param merchantCode
     * @param qrCode
     * @return
     * @throws IccardDatabaseException 
     * @see com.bcinfo.iccard.service.ComboCardTradeService#loadyByMerQrCode(java.lang.String, java.lang.String)
     */
    @Override
    public ComboCardExpensiveRecordTemp loadyByMerQrCode(String merchantCode, String qrCode)
            throws IccardDatabaseException
    {
        if(_logger.isDebugEnabled()){
            _logger.debug(MessageFormat.format("loadyByMerQrCode:merchantCode={0},qrCode={1}", merchantCode,qrCode));
        }
        try
        {
            ComboCardExpensiveRecordTemp ccsd=comboCardExpensiveRecordTempMapper.loadByMerQr(merchantCode, qrCode);
            return ccsd;
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new IccardDatabaseException();
        }
    }

    /* (non-Javadoc)
     * Description: <br/> 
     *
     * @param temp
     * @throws IccardDatabaseException 
     * @see com.bcinfo.iccard.service.ComboCardTradeService#saleComboCard(com.bcinfo.iccard.model.ComboCardExpensiveRecordTemp)
     */
    @Override
    public ComboCardExpensiveRecordTemp comboCardTrade(ComboCardExpensiveRecordTemp temp,Pos pos,String qrCode) throws IccardDatabaseException
    {
        if(_logger.isDebugEnabled()){
            _logger.debug("comboCardTrade:temp="+temp+",pos="+pos.toString()+",qrCode="+qrCode);
        }
        try
        {
          //人工干预状态直接修改为无效
            if(temp!=null && temp.getExpensiveStatus()==2){
                temp.setExpensiveStatus(3);
                temp.setUpdateTime(new Date());
                this.update(temp);
            }
            //新增预交易
            temp.setExpensiveStatus(0);
            temp.setMerchantEncode(pos.getMerchantCode());
            temp.setPosCode(pos.getPosCode());
            temp.setTwoDimensionCode(qrCode);
            temp.setUpdateTime(new Date());
            comboCardExpensiveRecordTempMapper.insertSelective(temp);
            return temp;
        }
        catch (DataAccessException e)
        {
            // TODO: handle exception
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new IccardDatabaseException();
        }
    }
}
